﻿using ProductTool.Dao;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ProductTool.Service
{
    public class JDInventoryService
    {
        ItemDao dao = new ItemDao();
        public async Task ProcessAsync() {

            while (true)
            {
                try
                {
                    var list = dao.getJDItemNoInv();
                    if (list.Count == 0)
                    {
                        Console.WriteLine("全部执行完毕");
                        break;
                    }
                    foreach (var item in list)
                    {
                        var stock = await getStockAsync(item);
                        if (!string.IsNullOrEmpty(stock))
                        {
                            dao.CreateJDItemInv(item, stock);
                        }
                    }
                }
                catch (Exception)
                {

                    Console.WriteLine("err,retry....");
                }
            }            
        }

        private async Task<string> getStockAsync(string sku)
        {
            string stock = string.Empty;
            using (HttpClient client = new HttpClient()) {
                var url = $"https://item.m.jd.com/item/mview2?datatype=1&cgi_source=mitem&sku={sku}";
                var message = new HttpRequestMessage(HttpMethod.Get, url);
                message.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36");
                message.Headers.Add("Cookie", "wxa_level=1; retina=0; cid=9; webp=1; __jda=122270672.15845016453661168933031.1584501645.1584501645.1584501645.1; __jdv=122270672%7Cdirect%7C-%7Cnone%7C-%7C1584501645366; __jdc=122270672; mba_muid=15845016453661168933031; autoOpenApp_downCloseDate_auto=1584501645816_21600000; sc_width=1920; shshshfpb=1173b9277ce6e4546a4d564c526a1154b4ec513999883c3325b07620b0; shshshfp=3b4eed63e2217bd9a2d85dc0ea36acbe; shshshfpa=c409b84d-f5f5-9104-d80a-3c91658357b9-1584501646; visitkey=53621430692692653; sk_history=39674374824%2C; commonAddress=0; mitemAddrName=; wqmnx1=MDEyNjM2M3B0ai92Y3c9MyZVZGQyOTVsMGRUOzQpZXQzVGljaDY0MHIuMllhLTQxUlMjISk%3D; __jdb=122270672.3.15845016453661168933031|1.1584501645; mba_sid=15845016453685792311208260918.3; __wga=1584501684526.1584501645841.1584501645841.1584501645841.3.1; shshshsID=06fbbdaacb3d73cb98eaffc89d0bb4dc_3_1584501684789; wq_addr=0%7C27_2376_4343_53946%7C%u9655%u897F_%u897F%u5B89%u5E02_%u96C1%u5854%u533A_%u5927%u96C1%u5854%u8857%u9053%7C%7C; jdAddrId=27_2376_4343_53946; jdAddrName=%u9655%u897F_%u897F%u5B89%u5E02_%u96C1%u5854%u533A_%u5927%u96C1%u5854%u8857%u9053; regionAddress=27%2C2376%2C4343%2C53946; mitemAddrId=27_2376_4343_53946; PPRD_P=UUID.15845016453661168933031-LOGID.1584501715849.1002546883; warehistory='12770380, 39674374824,'; wq_logid=1584501709.999485111;");
                stock = await client.SendAsync(message).Result.Content.ReadAsStringAsync();
                var match=Regex.Match(stock, "\"StockStateName\":\".+?\"");
                if (match.Success)
                {
                    stock = match.Value.Split(':')[1];
                    Console.WriteLine($"{sku} is OK");
                }
            }
            return stock;
        }
    }
}
